package android.os;

public class Message {
    public int what;
    public int arg1;
    public int arg2;
    public Object obj;
    //微秒
    long when;
    Handler target;
    Runnable callback;
    Message next;

    //使用消息池，实现反复利用
    private static final Object POOL_LOCK = new Object();
    private static final int MAX_POOL_SIZE = 50;
    private static int poolSize = 0;
    private static Message sPool;

    public Message() {
    }

    public static Message obtain(Handler target) {
        Message msg = obtain();
        msg.target = target;
        return msg;
    }

    public static Message obtain(Handler target, int what, int arg1, int arg2, Object obj) {
        Message msg = obtain();
        msg.target = target;
        msg.what = what;
        msg.arg1 = arg1;
        msg.arg2 = arg2;
        msg.obj = obj;
        return msg;
    }

    public static Message obtain(Handler target, Runnable callback) {
        Message msg = obtain();
        msg.target = target;
        msg.callback = callback;
        return msg;
    }

    public static Message obtain(Handler target, int what) {
        Message msg = obtain();
        msg.target = target;
        msg.what = what;
        return msg;
    }

    private static Message obtain() {
        if (sPool != null) {
            synchronized (POOL_LOCK) {
                Message m = sPool;
                sPool = sPool.next;
                m.next = null;
                poolSize--;
                return m;
            }
        }
        return new Message();
    }

    public void recycleUnchecked() {
        what = 0;
        arg1 = 0;
        arg2 = 0;
        obj = null;
        target = null;
        callback = null;
        next = null;

        synchronized (POOL_LOCK) {
            if (poolSize < MAX_POOL_SIZE) {
                next = sPool;
                sPool = this;
                poolSize++;
            }
        }
    }

    public String toString() {
        return toString(System.currentTimeMillis());
    }

    String toString(long now) {
        StringBuilder b = new StringBuilder();
        b.append("{ when=");
        //TODO:
//        TimeUtils.formatDuration(when - now, b);
        b.append((when - now));

        if (target != null) {
            if (callback != null) {
                b.append(" callback=");
                b.append(callback.getClass().getName());
            } else {
                b.append(" what=");
                b.append(what);
            }

            if (arg1 != 0) {
                b.append(" arg1=");
                b.append(arg1);
            }

            if (arg2 != 0) {
                b.append(" arg2=");
                b.append(arg2);
            }

            if (obj != null) {
                b.append(" obj=");
                b.append(obj);
            }

            b.append(" target=");
            b.append(target.getClass().getName());
        } else {
            b.append(" barrier=");
            b.append(arg1);
        }

        b.append(" }");
        return b.toString();
    }

    public Handler getTarget() {
        return target;
    }

    public Runnable getCallback() {
        return callback;
    }
}